package com.cookie; /**
 * @author wzm
 * @create 2023-06-29 11:52
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/sa")
public class ServletA extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //当访问当前servlet通知浏览器存储一个cookie的值

        //1.创建cookie的对象 往对象中放入值
        Cookie cookie01 = new Cookie("username","zs");
        Cookie cookie02 = new Cookie("password", "123");

        //设置cookie的存活时间
        //正数存活指定秒  负数表示浏览器关闭即销毁  0表示立即删除
        cookie01.setMaxAge(60*60*24*7);//存活一个周

        //2.通知浏览器将cookie保存下来
        response.addCookie(cookie01);
        response.addCookie(cookie02);


        response.getWriter().write("恭喜你，cookie保存到浏览器成功了");
    }
}
